package org.wxd.sign.web;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.wxd.sign.annotation.SignApi;
import org.wxd.sign.annotation.SignAttr;
import org.wxd.sign.annotation.SignModel;
import org.wxd.sign.annotation.SignType;
import org.wxd.sign.utils.Sha1Utils;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
public class DemoController {

    @SignApi(signType = SignType.INPUT)
    @GetMapping("/get")
    public String get(String code,HttpServletRequest request){
        log.info(code);
        return "get";
    }

    @SignApi(signType = SignType.INPUT)
    @GetMapping("/get2")
    public String get2(HttpServletRequest request,String code){
        log.info(code);
        return "get2";
    }


    @SignApi(signType = SignType.INPUT)
    @GetMapping("/get3")
    public Demo get2(HttpServletRequest request,Demo2 demo,String code){
        log.info(code);
        return null;
    }



    @Data
    public static class Demo{
        String code;
        String signature;
    }

    @SignModel
    @Data
    public static class Demo2{
        @SignAttr
        String demoCode;
    }

    @GetMapping("/list")
    public List<Demo> list(){
        Demo demo = new Demo();
        demo.code = "123";
        demo.signature = Sha1Utils.getSha1(demo.code);

        List<Demo> list = new ArrayList();

        Demo demo1 = new Demo();
        demo1.code = "124";
        demo1.signature = Sha1Utils.getSha1(demo.code);

        list.add(demo);
        list.add(demo1);

        return list;
    }


    @SignApi(signType = SignType.ALL)
    @GetMapping("/info")
    public void info(){

    }


    @Data
    public static class Post{
        String code;
        PostInner inner;
    }
    @Data
    public static class PostInner{
        String name;
        String code;
    }

    @SignApi(signType = SignType.INPUT)
    @PostMapping("/postDemo")
    public Post postDemo(Post post){
        log.info("{}",post);
        return post;
    }



}
